package org.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.bean.Book;
import org.example.mapper.BookMapper;
import org.example.service.BookService;
import org.example.util.JsonResult;
import org.example.util.ResultTool;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BookServiceImpl extends ServiceImpl<BookMapper, Book> implements BookService {
    @Override
    public JsonResult findlowerBook() {
        QueryWrapper<Book> wrapper = new QueryWrapper<>();
        wrapper.orderByAsc("price");
        wrapper.last("limit 4");
        List<Book> list = list(wrapper);
        return ResultTool.success(list);
    }

    @Override
    public JsonResult findFreshBook() {
        QueryWrapper<Book> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("create_time");
        wrapper.last("limit 5");
        List<Book> list = list(wrapper);
        return ResultTool.success(list);
    }

    @Override
    public JsonResult findlowerBookMore(int page) {
        Page<Book> of = Page.of(page, 10);
        QueryWrapper<Book> wrapper = new QueryWrapper<>();
        wrapper.orderByAsc("price");
        List<Book> list = list(of, wrapper);
        of.setRecords(list);
        return ResultTool.success(of);
    }

    @Override
    public JsonResult findFreshBookMore(int page) {
        Page<Book> of = Page.of(page, 10);
        QueryWrapper<Book> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("create_time");
        List<Book> list = list(of, wrapper);
        of.setRecords(list);
        return ResultTool.success(of);
    }

    @Override
    public JsonResult findSearchByBookName(String name, int page) {
        Page<Book> of = Page.of(page, 10);
        QueryWrapper<Book> wrapper = new QueryWrapper<>();
        wrapper.like("name", name);
        List<Book> list = list(of, wrapper);
        of.setRecords(list);
        return ResultTool.success(of);
    }

    @Override
    public JsonResult findCategoryBook(String categoryId, int page) {
        Page<Book> of = Page.of(page, 10);
        QueryWrapper<Book> wrapper = new QueryWrapper<>();
        wrapper.like("category_id", categoryId);
        List<Book> list = list(of, wrapper);
        of.setRecords(list);
        return ResultTool.success(of);
    }
}
